<?php
    include_once ("MySql.php");
    include_once ("../entity/OrdenCompra.php");
    include_once ("../entity/ProductoAlmacen.php");
    
    class OrdenCompraDA extends dal{
        public function OrdenCompraDA(){
            parent::dal();
        }
        
        public function numeroRegistro1($num_doc){
            $sql = "CALL SGLSS_CUENTA_DETALLE_ORDENCOMPRA('$num_doc')";
            $result = parent::ejecutar($sql);
            $num_row=0;
            while($fila = mysqli_fetch_object($result)){
                $num_row= $fila->num_row;
            }
            return $num_row;
        }
        
        public function BuscarSearch1($entity, $start , $limit,$sidx,$sord){
            $sql = "CALL SGLSS_DETALLE_ORDENCOMPRA_SEARCH('$entity->num_doc',"
                    . "'$start' ,"
                    . "'$limit',"
                    . "'$sidx',"
                    . "'$sord')";
            $result = parent::ejecutar($sql);
            while($fila = mysqli_fetch_object($result)){
                $obj = new OrdenCompra();
                $obj->codigo = $fila->codigo;
                $obj->cod_producto_almacen = $fila->cod_producto_almacen;
                $obj->producto = $fila->producto;
                $obj->cantidad = $fila->cantidad;
                $obj->cod_proveedor = $fila->cod_proveedor;
                $obj->proveedor = $fila->proveedor;
                $detalleOrdenCompra[] = $obj;
            }
            return $detalleOrdenCompra;
        }
        
        public function obtenerID($entity){
            $sql = "CALL SGLSS_DETALLE_ORDENCOMPRA_X_COD('$entity->num_doc')";
            $result = parent::ejecutar($sql);
            
            while ($fila = mysqli_fetch_object($result)){
                $obj = new OrdenCompra();
                $obj->num_doc = $fila->num_doc;
                $obj->responsable = $fila->responsable;
                $obj->desc_responsable = $fila->desc_responsable;
                $obj->fecha_emision = $fila->fecha_emision;
            }
            return $obj;
        }

        public function numeroRegistro($num_doc,$producto,$fecha){
            $sql = "CALL SGLSS_CUENTA_ORDENCOMPRA('$num_doc','$producto','$fecha')";
            $result = parent::ejecutar($sql);
            $num_row=0;
            while($fila = mysqli_fetch_object($result)){
                $num_row= $fila->num_row;
            }
            return $num_row;
        }
        
        public function BuscarSearch($entity, $start , $limit,$sidx,$sord){
            $sql = "CALL SGLSS_ORDENCOMPRA_SEARCH('$entity->num_doc',"
                    . "'$entity->producto',"
                    . "'$entity->fecha_emision',"
                    . "'$start' ,"
                    . "'$limit',"
                    . "'$sidx',"
                    . "'$sord')";
            $result = parent::ejecutar($sql);
            while($fila = mysqli_fetch_object($result)){
                $obj = new OrdenCompra();
                $obj->num_doc = $fila->num_doc;
                $obj->fecha_emision = $fila->fecha_emision;
                $obj->responsable = $fila->responsable;
                $obj->desc_responsable = $fila->desc_responsable;
                $ordenCompra[] = $obj;
            }
            return $ordenCompra;
        }
        
        public function listarXProductoAlmacen($cod_producto_almacen){
            $sql= "CALL SGLSS_PRODUCTOALMACEN_X_CODIGO('$cod_producto_almacen')";
            $result = parent::ejecutar($sql);
            
            while($fila = mysqli_fetch_object($result)){
                $obj = new ProductoAlmacen();
                $obj->codigo = $fila->codigo;
                $obj->cod_almacen = $fila->cod_almacen;
                $obj->almacen = $fila->almacen;
                $obj->cod_producto = $fila->cod_producto;
                $obj->producto = $fila->producto;
                $obj->cod_proveedor = $fila->cod_proveedor;
                $obj->proveedor = $fila->proveedor;
                $obj->cantidad = 0;
                $obj->stock_min = $fila->stock_min;
                $obj->stock_max = $fila->stock_max;
                $obj->stock_actual = $fila->stock_actual;
//                $prodAlmacen[] = $obj;
            }
            return $obj;
        }
        public function listarXAlmacen($cod_almacen){
            $sql = "CALL SGLSS_PRODUCTOALMACEN_X_ALMACEN('$cod_almacen')";
            $result = parent::ejecutar($sql);
//            echo $sql;
            while ($fila = mysqli_fetch_object($result)){
                if($fila->pendiente == null || ($fila->pendiente == 0 && $fila->cantidad <= $fila->stock_min)){
                    $obj = new ProductoAlmacen();
                    $obj->codigo = $fila->codigo;
                    $obj->cod_almacen = $fila->cod_almacen;
                    $obj->almacen = $fila->almacen;
                    $obj->cod_producto = $fila->cod_producto;
                    $obj->producto = $fila->producto;
                    $obj->cod_proveedor = $fila->cod_proveedor;
                    $obj->proveedor = $fila->proveedor;
                    $obj->cantidad = 0;
                    $obj->stock_min = $fila->stock_min;
                    $obj->stock_max = $fila->stock_max;
                    $obj->stock_actual = $fila->stock_actual;
                    $prodAlmacen[] = $obj;
                }
            }
            return $prodAlmacen;
        }
        public function generarCodigo(){
            $sql = "CALL SGLSS_ORDEN_COMPRA_CODIGO";
            $result = parent::ejecutar($sql);
            
            while ($fila = mysqli_fetch_object($result)){
                $obj = new OrdenCompra();
                $obj->codigoNuevo = $fila->codigoNuevo;
            }
            return $obj;
        }
        public function insertarOrdenCompra($codigo,$fecha,$usuario_crea){
            $sql = "CALL SGLSI_ORDEN_COMPRA('$codigo','$fecha','$usuario_crea')";
            $result = parent::ejecutar($sql);
//            echo $sql;
            if($result){
                return true;
            }else{
                return false;
            }
        }
        public function insertarOrdenCompraDetalle($codigo,$detaOrden){
            $sql = "CALL SGLSI_ORDEN_COMPRA_DETALLE('$codigo',"
                    . "'$detaOrden->codigo',"
                    . "'$detaOrden->cantidad',"
                    . "'$detaOrden->cod_proveedor')";
            $result = parent::ejecutar($sql);
            if($result){
                return true;
            }else{
                return false;
            }
        }
    }
?>